package com.ioudi.digu.aop;


import com.ioudi.digu.service.IUserAwareSupport;
import com.ioudi.digu.service.drm.IDrmService;

import flex.messaging.FlexContext;

public class FlexUserAwarer
{
    public void awareUser(Object service) throws Throwable
    {
    	Object raw = FlexContext.getFlexClient().getAttribute("user");
    	String user = raw == null ? null : String.valueOf(raw);
    	if(service instanceof IDrmService)
    	{
    		IDrmService ds = (IDrmService)service;
    		ds.setUser(user);	
    	}
    	else if(service instanceof IUserAwareSupport)
    	{
    		IUserAwareSupport uas = (IUserAwareSupport)service;
    		if(!uas.isUserAwared())
    		{
    			uas.setUser(user);
    		}
    		uas.setUserAwared(true);
    	}
    }
    
    public void releaseUser(Object service)
    {
    	if(service instanceof IUserAwareSupport)
    	{
    		IUserAwareSupport uas = (IUserAwareSupport)service;
    		uas.setUserAwared(false);
    	}
    }
}
